package de.haw.dbp.a4.playground;

import java.awt.Component;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.AbstractTableModel;



import dc.connectionmanager.database.DBConnectionManager;
import dc.connectionmanager.logic.ACommand;
import dc.connectionmanager.logic.CArgument;
import dc.connectionmanager.logic.ICallback;
import dc.connectionmanager.logic.IConnectionManager;

public class JPanelShowMedia extends JPanel 
{
	private static final long serialVersionUID = -6481626189775850394L;
	private JFrame parent;
	private DBConnectionManager dbcm;
	private DBCommand command;
	
	
	private JScrollPane sp_container;
	private JTable tb_list;
	private JTextField tf_filter_medianame;
	private JTextField tf_filter_mediatyp;
	private JButton b_send;
	
	private ArrayListTableModel arrList;
	
	private boolean initSearched = false;
	
	public JPanelShowMedia(JFrame pa, DBConnectionManager pCM) 
	{
		super();
		
		this.parent = pa;
		this.dbcm = pCM;
		command = new DBCommand(this.dbcm);
		
		this.initGUIElements();
	}

	@Override
	public void setEnabled(boolean enabled) 
	{
		super.setEnabled(enabled);
		for(Component com : this.getComponents())
		{
			com.setEnabled(enabled);
		}
		
		if(!initSearched)
		{
			if(enabled)
			{
				searchMedia();
			}
		}
	}
	
	private void initGUIElements()
	{
		//Panel einrichten
		this.setLayout(null);
		this.setSize(parent.getSize());
		this.setLocation(0,0);
		
		//Textfeld1 einrichten
		this.tf_filter_medianame = new JTextField();
		this.tf_filter_medianame.setSize(200, 20);
		this.tf_filter_medianame.setLocation(10, 10);
		
		//Textfeld2 einrichten
		this.tf_filter_mediatyp = new JTextField();
		this.tf_filter_mediatyp.setSize(200, 20);
		this.tf_filter_mediatyp.setLocation(this.tf_filter_medianame.getSize().width + 20, 10);
		
		//Button einrichten
		this.b_send = new JButton("Senden");
		this.b_send.setSize(100, 20);
		this.b_send.setLocation(this.tf_filter_mediatyp.getLocation().x + this.tf_filter_mediatyp.getSize().width + 10, 10);
		
		//ScrollPane einrichten
		sp_container = new JScrollPane();
		sp_container.setSize(this.getSize().width - 50, this.getSize().height - 70);
		sp_container.setLocation(0, 100);
		
		//Tabelle einrichten
		tb_list = new JTable();
		tb_list.setSize(sp_container.getSize());

		this.sp_container.add(tb_list);
		this.add(tf_filter_medianame);
		this.add(tf_filter_mediatyp);
		this.add(b_send);
		this.add(sp_container);
		
		
		//ActionListener für Textfelder
		tf_filter_medianame.getDocument().addDocumentListener(new DocumentListener() 
		{
			
			@Override
			public void removeUpdate(DocumentEvent arg0) 
			{
				searchMedia();
				
			}
			
			@Override
			public void insertUpdate(DocumentEvent arg0) 
			{
				searchMedia();
				
			}
			
			@Override
			public void changedUpdate(DocumentEvent arg0) 
			{
				searchMedia();
				
			}
		});
	}
	
	private void searchMedia()
	{
		this.command.getMedia(new ICallback() 
		{
			
			@Override
			public void handleResult(Object arg0) 
			{
				if(arg0 != null)
				{
					renderTable((ArrayList<ArrayList<String>>) arg0);
				}
			}
		}, tf_filter_medianame.getText(), tf_filter_mediatyp.getText());
	}
	
	private void renderTable(ArrayList<ArrayList<String>> values)
	{
		arrList = new ArrayListTableModel(values);
		
		this.tb_list.setModel(arrList);
	}
	
	class DBCommand extends ACommand
	{

		public DBCommand(IConnectionManager cmHandle) 
		{
			super(cmHandle);
		}
		
		public void exec_query(ICallback cb, String query)
		{
			this.sendCommand(new CArgument(cb, query));
		}
		
		public void getMedia(ICallback cb, String m_name, String m_type)
		{
			//this.sendCommand(new CArgument(cb, "SELECT Medium.Mediumname, Medium.Hersteller, MEDIENTYP.Name AS MedienTyp FROM Medium INNER JOIN MEDIENTYP ON Medium.MTyp = MEDIENTYP.MedienTyp_ID WHERE Medium.Mediumname LIKE '%"+m_name+"%' AND MEDIENTYP.Name LIKE '%"+m_type+"%' ORDER BY MEDIENTYP.Name ASC"));
			  this.sendCommand(new CArgument(cb, "SELECT medium.mediumname, medium.hersteller, medientyp.name as medientyp from medium INNER JOIN medientyp on medium.mtyp = medientyp.medientyp_id WHERE medium.mediumname LIKE '%"+m_name+"%' AND medientyp.name like '%"+m_type+"%' ORDER BY medientyp.name ASC"));
		}
		
	}
	
	class ArrayListTableModel extends AbstractTableModel
	{
		private static final long serialVersionUID = -8190693509401280367L;
		private ArrayList<ArrayList<String>> data;
		private ArrayList<String> cNames;
		
		public ArrayListTableModel(ArrayList<ArrayList<String>> rData)
		{
			this.data = rData;
			try
			{
				this.cNames = this.data.get(0);
				this.data.remove(0);
			}
			catch(Exception ex)
			{
				this.cNames = new ArrayList<String>();
			}
			
			
		}

		@Override
		public int getColumnCount() 
		{
			try
			{
				return this.data.get(0).size();
			}
			catch(Exception ex)
			{
				return 0;
			}
		}

		@Override
		public int getRowCount() 
		{
			try
			{
				return this.data.size();
			}
			catch(Exception ex)
			{
				return 0;
			}
		}

		@Override
		public Object getValueAt(int rowIndex, int columnIndex) 
		{
			try
			{
				return this.data.get(rowIndex).get(columnIndex);
			}
			catch(Exception ex)
			{
				return new Object();
			}
		}
		
		@Override
		public void setValueAt(Object aValue, int rowIndex, int columnIndex) 
		{
			super.setValueAt(aValue, rowIndex, columnIndex);
			try
			{
				this.data.get(rowIndex).set(columnIndex, (String) aValue);
			}
			catch(Exception ex)
			{ }
			this.fireTableDataChanged();
		}
		
		@Override
		public String getColumnName(int column) 
		{
			return this.cNames.get(column);
		}
	}
	
	
}
